﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using SBlogCore.Common.App.Options;

namespace SBlogCore.Common.App.Manager
{
    public static class OptionsExtensions
    {
        public static void OptionsSetUp(this IServiceCollection services)
        {
            //将appsettings.json转化为强类型
            var con = new ConfigurationManager().AddJsonFile(path: "appsettings.json", optional:false,reloadOnChange:true).Build();
            services.AddOptions()
                .Configure<AppsettingsOptions>(con)
                .BuildServiceProvider()
                .GetRequiredService<IOptionsMonitor<AppsettingsOptions>>()
                .OnChange(p => {
                    AppsettingOptionsSingle.Change(p);
                    Console.WriteLine($"文档发生变化{JsonConvert.SerializeObject(p)}");
                });
        }
    }
}
